// 设置活跃页面
class PageActivityMaintainer {
    constructor() {
        this.interval = 5000;
        this.activeTabs = new Map(); // 存储用户手动激活的标签页
        this.timers = new Map(); // 存储定时器
    }

    init() {

        // 监听来自popup的消息
        chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
            if (request.action === 'toggleActivity') {
                this.toggleTabActivity(request.tabId, request.enable);
                sendResponse({ success: true });
            }
            if (request.action === 'checkStatus') {
                sendResponse({ isActive: this.activeTabs.get(request.tabId) });
            }
            if (request.action === 'getActiveTabs') {
                sendResponse(this.getActiveTabs());
            }
            if (request.action === 'clearAllTabs') {
                this.clearAllTabs();
            }
            return true;
        });

        // 清理已关闭标签页的定时器
        chrome.tabs.onRemoved.addListener((tabId) => {
            this.clearTabTimer(tabId);
        });
    }

    // 切换标签页活跃状态
    toggleTabActivity(tabId, enable) {
        if (enable) {
            if (!this.timers.has(tabId)) {
                const timer = setInterval(() => {
                    this.simulateActivity(tabId);
                }, this.interval);
                this.timers.set(tabId, timer);
                this.activeTabs.set(tabId, true);
            }
        } else {
            this.clearTabTimer(tabId);
        }
    }

    // 模拟活动
    async simulateActivity(tabId) {
        try {
            await chrome.scripting.executeScript({
                target: { tabId },
                func: () => {
                    const activities = [
                        () => {
                            // 模拟鼠标轨迹
                            const steps = 5 + Math.floor(Math.random() * 10);
                            for (let i = 0; i < steps; i++) {
                                setTimeout(() => {
                                    window.dispatchEvent(new MouseEvent('mousemove', {
                                        clientX: Math.random() * window.innerWidth,
                                        clientY: Math.random() * window.innerHeight,
                                        bubbles: true
                                    }));
                                }, i * 200);
                            }
                        },
                        () => {
                            // 自然滚动
                            const distance = 100 + Math.random() * 500;
                            window.scrollBy({
                                top: Math.random() > 0.5 ? distance : -distance,
                                behavior: 'smooth'
                            });
                        },
                        () => document.activeElement?.blur()
                    ];
                    activities[Math.floor(Math.random() * activities.length)]();
                }
            });
        } catch (error) {
            console.error('Activity simulation failed:', error);
            this.clearTabTimer(tabId);
        }
    }

    // 清理定时器
    clearTabTimer(tabId) {
        if (this.timers.has(tabId)) {
            clearInterval(this.timers.get(tabId));
            this.timers.delete(tabId);
            this.activeTabs.delete(tabId);
        }
    }

    // 清除方法
    clearAllTabs() {
        this.timers.forEach((timer, tabId) => {
            clearInterval(timer);
        });
        this.timers.clear();
        this.activeTabs.clear();
    }

    // 获取活跃标签页状态
    getActiveTabs() {
        return Array.from(this.activeTabs.keys());
    }
}

const maintainer = new PageActivityMaintainer();
maintainer.init();




// 创建右键菜单
chrome.runtime.onInstalled.addListener(() => {
    chrome.contextMenus.create({
        id: "formatJson",
        title: "格式化JSON预览",
        contexts: ["selection"]
    });
});

// 监听右键菜单点击
chrome.contextMenus.onClicked.addListener((info, tab) => {
    if (info.menuItemId === "formatJson" && info.selectionText) {
        // 向内容脚本发送消息
        chrome.tabs.sendMessage(tab.id, {
            action: "showJsonPreview",
            selectedText: info.selectionText
        });
    }
});


































